package cn.yunhe.server;

import cn.yunhe.dao.IFriendDao;
import cn.yunhe.entity.FriendMessage;
import cn.yunhe.entity.Myfriend;
import cn.yunhe.entity.User;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FriendServer {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    IFriendDao friendDao = session.getMapper(IFriendDao.class);

    /**
     * 查询好友
     * @param user
     * @return
     */
    public List<Myfriend> SelectAllFriend(User user){
        return friendDao.SelectAllFriend(user);
    }

    /**
     * 发送添加好友请求
     * @param myfriend
     * @return
     */
    public boolean AddFriend(Myfriend myfriend){
        int flag = friendDao.AddFriend(myfriend);
        if (flag>0){
            session.commit();
            return true;
        }else {
            return false;
        }
    }


    /**
     * 接受好友请求添加好友
     * @param friendMessage 好友消息对象
     * @return
     */
    public boolean EnsureAddFriend(FriendMessage friendMessage){
        boolean flag = friendDao.EnsureAddFriend(friendMessage);
        if (flag){
            session.commit();
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取好友消息对象集合
     * @param user 好友对象
     * @return 好友消息集合
     */
    public List<FriendMessage> GetFriendMessage(User user){
        return friendDao.GetFriendMessage(user);
    }

    /**
     * 删除好友添加信息
     * @param friendMessage
     * @return
     */
    public boolean DeleteFriendMessage(FriendMessage friendMessage){
        boolean flag = friendDao.DeleteFriendMessage(friendMessage);
        if (flag){
            session.commit();
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除好友方法
     * @param myfriend 好友对象
     * @return
     */
    public boolean DeleteFriend(Myfriend myfriend){
        System.out.println(myfriend.getFuName()+"99999999"+myfriend.getuName());
        int flag = friendDao.DeleteFriend(myfriend);
        if (flag>0){
            session.commit();
            return true;
        }else {
            return false;
        }
    }
}
